package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.SPUtils;
import com.luck.picture.lib.dialog.PhotoItemSelectedDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.SelectProjectAdapt;
import com.zxkj.commonpatrol.databinding.ActivitySelectProjectBinding;
import com.zxkj.commonpatrol.model.entity.SelectProjectEntrity;
import com.zxkj.commonpatrol.model.response.SearchProjectResponse;
import com.zxkj.commonpatrol.viewmodel.SelectProjectsViewModel;

import java.util.ArrayList;
import java.util.List;
@Route(path = "/Common/SelectProjectActivity")
public class SelectProjectActivity extends BaseVmDataBindingActivity<ActivitySelectProjectBinding, SelectProjectsViewModel> {

    private RecyclerView recyclerView;
    private SelectProjectAdapt adapt;
    private int PageSize = 50;
    private int PageIndex = 1;
    private  String Filter;
    private SmartRefreshLayout smartRefreshLayout;
    private  boolean isShow=false;
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_select_project;
    }

    @Override
    public int initVariableId() {
        return BR.SelectProjectsViewModel;
    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.ivSearch.setOnClickListener(v->{
            binding.multipleRejectStatusView.showLoading();
            Filter=binding.edSeach.getTextEx().toString().trim();
            viewModel.getSelectData("",Filter,PageIndex,PageSize);
        });

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getSelectData("",Filter,PageIndex,PageSize);
            }
        });
        binding.multipleRejectStatusView.setOnRetryClickListener(v->{
            binding.multipleRejectStatusView.showLoading();
            viewModel.getSelectData("",Filter,PageIndex,PageSize);
        });

        binding.edSeach.setOnFocusChangeListener((v,h)->{
            if (h){
                binding.ivSearch.setImageResource(R.drawable.search_biss);
            }else{
                binding.ivSearch.setImageResource(R.drawable.search);
            }
        });
        binding.btnSearch.setOnClickListener(v->{
            if (!isShow){
                binding.rlUpcoming.setVisibility(View.VISIBLE);
                isShow=true;
            }else{
                binding.rlUpcoming.setVisibility(View.GONE);
                isShow=false;
                Filter="";
                binding.edSeach.setTextEx("");
            }
        });
    }

    @Override
    protected void initUI() {
        LinearLayoutManager layoutManager =new LinearLayoutManager(this);
         recyclerView = (RecyclerView) findViewById(R.id.recy_select_project);
         recyclerView.setLayoutManager(layoutManager);


         smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);

    }

    @Override
    protected void initData() {
         binding.multipleRejectStatusView.showLoading();
         viewModel.getSelectData("",Filter,PageIndex,PageSize);
//         viewModel.text();

    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.data.observe(this, new Observer<StateData<List<SearchProjectResponse.RowsBean>>>() {
            @Override
            public void onChanged(StateData<List<SearchProjectResponse.RowsBean>> listStateData) {
                smartRefreshLayout.finishRefresh(/*,false*/);//传入false表示刷新失败


               if(listStateData.getStatus()==StateData.DataStatus.ERROR){
                    binding.multipleRejectStatusView.showError();
                }else{
                   if (listStateData.getData().size()==0||listStateData.getData()==null){
                       //空数据
                       binding.multipleRejectStatusView.showEmpty();
                   }else{
                       binding.multipleRejectStatusView.showContent();
                       if (adapt==null){
                           adapt =new SelectProjectAdapt(R.layout.item_select_projects,listStateData.getData());
                           recyclerView.setAdapter(adapt);
                           adapt.setOnItemClickListener((adapter, view, position) -> {
                               Bundle bundle=new Bundle();

                               bundle.putString("ProCode",adapt.getData().get(position).getCode());
                               bundle.putString("ProName",adapt.getData().get(position).getName());
//                               SPUtils.put(ItrackConstant.ProjectName,adapt.getData().get(position).getName());
                               startActivity(ProjectListActivity.class,bundle);

                           });
                       }else{
                           adapt.setNewData(listStateData.getData());
                           recyclerView.setAdapter(adapt);
                       }
                   }


                }



            }
        });
    }
}
